//
// Created by bleedingfight on 2020/7/9.
//
#include "fourSumCount.h"
map<int,int> sum_map(vector<int>&a,vector<int>&b,bool flag){
    map<int,int>result;
    int sum_value = 0;
    for(int i=0;i<a.size();i++){
        for(int j=0;j<b.size();j++){
            if(flag) sum_value = a[i]+b[j];
            else
                sum_value = -(a[i]+b[j]);
            vector<int>sum_indexs = {i,j};
            result[sum_value] += 1;
        }
    }
    return result;
}
int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {
    int result=0;
    map<int,int> sum1 = sum_map(A,B,true);
    map<int,int> sum2 = sum_map(C,D, false);
    for(auto pair1:sum1){
        int key = pair1.first;
        result+= pair1.second*sum2[key];
    }
    return result;
}
